package com.yc.web.servlets;

import com.yc.bean.Resfood;
import com.yc.model.CartItem;
import com.yc.model.JsonModel;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-06-13 14:32
 */
@WebServlet("/resorder.action")
public class ResOrderServlet extends BaseServlet {
    protected void getCartInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JsonModel jm=new JsonModel();
        HttpSession session=req.getSession();
        if (session.getAttribute("cart")!=null){
            Map<Integer, CartItem>cart= (Map<Integer, CartItem>) session.getAttribute("cart");
            jm.setCode(1);
            jm.setObj(cart.values());
        }else {
            jm.setCode(0);
        }
        super.writeJson(jm,resp);
    }
    protected void clearAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        JsonModel jm=new JsonModel();
        HttpSession session=req.getSession();
        session.removeAttribute("cart");
        jm.setCode(1);
        super.writeJson(jm,resp);
    }
    protected void order(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        JsonModel jm = new JsonModel();
        //1.判断是否登录
        HttpSession session = req.getSession();
//        if (session.getAttribute("resuser") == null) {
//            jm.setCode(0);
//            super.writeJson(jm, resp);
//            return;
//        }
        int fid = Integer.parseInt(req.getParameter("fid"));
        int num = Integer.parseInt(req.getParameter("num"));
        //根据fid查出  resfood对象  再放到购物车中
        //a.根据fid从数据库查
        String sql = "select*from resfood where fid=?";
        List<Resfood> list = dbHelper.select(Resfood.class, sql, fid);
        Resfood resfood = null;
        if (list != null && list.size() > 0) {
            resfood = list.get(0);
        }
        //b.根据fid从session中查 性能好 但是用与测试是未经过其他页面所以session没有存值

        //2.添加购物车   (1)判断购物车中是否已有 有：数量加一  没有：创建购物项
        Map<Integer, CartItem> cart = new HashMap<Integer, CartItem>();
        if (session.getAttribute("cart")!=null){
            cart= (Map<Integer, CartItem>) session.getAttribute("cart");
        }else {
            session.setAttribute("cart",cart);
        }
        CartItem ci=null;
        if (cart.containsKey(fid)){
            //如果为true  说明该物品在购物车已存在
            ci=cart.get(fid);
            int newNum=ci.getNum()+num;
            ci.setNum(newNum);
        }else {
            ci=new CartItem();
            //设置这个购物车的信息
            ci.setFood(resfood);
            ci.setNum(num);
        }
        //容错：
        if (ci.getNum()<=0){
            //如果数量小于等于0 删除该商品
            cart.remove(fid);
        }else {
            //计算小计
            ci.getSmallCount();
            //存入购物车
            cart.put(fid,ci);
        }
        session.setAttribute("cart",cart);
        jm.setCode(1);
        jm.setObj(cart.values());
        super.writeJson(jm,resp);
    }
}
